// pages/allShop/shopDetail/shopDetail.js
import {commodityDetail,cartAdd } from '../../../common/api/shop';
import dayjs from 'dayjs';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopId: '', // 产品id
    shopData: {
      galleryUrls: [],
      second: null,
    },
    indicatorDots:true,
    autoplay:true,
    interval:3000,
    records: [],
    historys: [],
    bannerData:[
      '../../../images/cart-blank.png',
      '../../../images/cart-blank.png',
    ],
    // 抽屉
    visible: false,
    placement: 'right',
    row: {}, // 弹出选中对象
    defaultValue: 0,
    notCart: false,
    pageType: 1, // 页面类型 1 正常商品 2 金币兑换跳转过来的
    notGold: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.shopId = options.id;
      console.log( options.type)
      if(options.type == 3){
        this.data.notGold = true;
      }
     // 3 是倒计时
    this.setData({
      pageType: options.type || 1,
      notCart: options.notCart?true:false
    })
    this.getCommodityDetail();
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  finish(){
    this.setData({
      "shopData.second": 0
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新');
    this.getCommodityDetail();
    // 在这里编写数据更新的逻辑
    setTimeout(() => {
      wx.stopPullDownRefresh(); // 数据更新完成后，调用该方法停止刷新
    }, 500);
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e){
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },

  // 获取商品详情
  async getCommodityDetail(){
    wx.showLoading({
      title: '加载中',
    })
    let res = await commodityDetail({id: this.data.shopId,orderId: ''});
    (res.records || []).forEach(e => {
      e.timeText = dayjs(e.createTime).format('YYYY-MM-DD HH:mm:ss')
    });
    (res.historys || []).forEach(e => {
      e.timeText = dayjs(e.createTime).format('YYYY-MM-DD HH:mm:ss')
    })
    res.totalPrice2 = res.totalPrice;
    console.log(res, '商品信息',res.records)

      this.setData({
        shopData: res,
        records: res.records,
        historys: res.historys
      });
      wx.hideLoading()
  },
  // 跳转到购买界面
  addOrder(){
    let idObj = JSON.stringify([{
      id: this.data.shopData.id,
      num: 1,
      type: this.data.notGold ?2:1
    }]);

    
    wx.navigateTo({
      url: "/pages/allShop/submitorder/submitorder?id=" + idObj
    })
  },
  // 添加购物车
  async addCart(){
    let user = wx.getStorageSync('userInfo');
    let data = {
      userid: user.id,
      goodsId: this.data.shopData.id,
      cartNumber: 1,
    }
    let res = await cartAdd(data);
    wx.showToast({
      title: '添加购物车成功~',
      icon: 'none'
    })
  },
  goParticulars(){
      wx.navigateTo({
        url: '/pages/allShop/graphicDetails/graphicDetails?id=' + this.data.shopId,
      })
  },
  onIconTap(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  showPouop(e){
    let row = e.currentTarget.dataset.item;
    row._type = e.currentTarget.dataset.type;
    
    if(!row.numArr){
      // let beginNum = row.beginNum || 0;
      // let endNum = row.endNum || 10000000;
      // let arr = [];
      // console.log(endNum,beginNum)
      // for(let i=beginNum;i<=endNum;i++){
      //   arr.push(i)
      // };
      console.log(row.randomList, "zxc",row)
      row.numArr = row.randomList;
    }
    if(row._type == 2){
      row.winTime2 = dayjs(row.winTime).format('YYYY-MM-DD HH:mm:ss')
    }
    console.log('点击了',row)
    this.setData({
      visible: true,
      row: row
    })
  },
  onVisibleChange(){
    console.log('弹出层出发')
    this.setData({
      visible: false
    })
  },
 
  onTabsChange(e){
    console.log('发生变化', e.detail)
    this.setData({
      defaultValue: e.detail.value
    })
  },

  // 购买
  buyOrder2(e){
    // console.log('点击购买')
    // let id = e.currentTarget.dataset.item.id;
    let idObj = JSON.stringify([{
      id: this.data.shopData.id,
      num: 1
    }]);
    wx.navigateTo({
      url: "/pages/allShop/submitorder2/submitorder2?id=" + idObj,
    })
  },
  amplification(e){
    wx.previewImage({
      current: e.target.dataset.img,
      urls: this.data.shopData.galleryUrls,
    })
  },
  // 历史换的者跳转详情事件
  goDetails(e){

    wx.navigateTo({
      url: '/packageA/shopDetail2/shopDetail2?id=' + this.data.shopId +'&orderId=' + e.currentTarget.dataset.item.id,
    })
  },
})